package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.health.dao.MemberDaoMP;
import com.itheima.health.dao.OrderDaoMP;
import com.itheima.health.dao.SetmealDaoMP;
import com.itheima.health.pojo.*;
import com.itheima.health.service.ReportService;
import com.itheima.health.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DecimalFormat;
import java.util.*;

@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
    private MemberDaoMP memberDao;
    @Autowired
    private OrderDaoMP orderDaoMP;
    @Autowired
    private SetmealDaoMP setmealDaoMP;


    @Override
    @Transactional
    public Map<String, Object> getBusinessReportData() throws Exception {
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        String day = DateUtil.parseDate2String(new Date());
        // 获取本周一日期
        String monday = DateUtil.parseDate2String(DateUtil.getThisWeekMonday());
        // 获取本月的第一天
        String firstDay4ThisMonth = DateUtil.parseDate2String(DateUtil.getFirstDay4ThisMonth());
        // 本日新增会员数
        Integer todayNewMember = memberDao.selectCount(new LambdaQueryWrapper<Member>().eq(Member::getRegTime, day));
        // 获取全部会员数
        Integer totalMember = memberDao.selectCount(null);
        // 本周新增会员数
        Integer thisWeekNewMember = memberDao.selectCount(new LambdaQueryWrapper<Member>().eq(Member::getRegTime, monday));
        // 本月新增会员数
        Integer thisMonthNewMember = memberDao.selectCount(new LambdaQueryWrapper<Member>().eq(Member::getRegTime, firstDay4ThisMonth));
        //今日预约数
        Integer todayOrderNumber = orderDaoMP.selectCount(new LambdaQueryWrapper<OrderMP>().eq(OrderMP::getOrderDate, day));
        //本周预约数
        Integer thisWeekOrderNumber = orderDaoMP.selectCount(new LambdaQueryWrapper<OrderMP>().eq(OrderMP::getOrderDate, monday));
        //本月预约数
        Integer thisMonthOrderNumber = orderDaoMP.selectCount(new LambdaQueryWrapper<OrderMP>().eq(OrderMP::getOrderDate, firstDay4ThisMonth));
        //今日到诊数
        Integer todayVisitsNumber;
        todayVisitsNumber = orderDaoMP.selectCount(new LambdaQueryWrapper<OrderMP>().eq(OrderMP::getOrderDate, day).eq(OrderMP::getOrderStatus, OrderMP.ORDERSTATUS_YES));
        //本周到诊数
        Integer thisWeekVisitsNumber = orderDaoMP.selectCount(new LambdaQueryWrapper<OrderMP>().eq(OrderMP::getOrderDate, monday).eq(OrderMP::getOrderStatus, OrderMP.ORDERSTATUS_YES));
        //本月到诊数
        Integer thisMonthVisitsNumber = orderDaoMP.selectCount(new LambdaQueryWrapper<OrderMP>().eq(OrderMP::getOrderDate, firstDay4ThisMonth).eq(OrderMP::getOrderStatus, OrderMP.ORDERSTATUS_YES));
        //热门套餐查询
        // 获取全部套餐预约数
        Double hotSetmeals = Double.valueOf(orderDaoMP.selectCount(null));
        QueryWrapper<OrderMP> queryWrapper = new QueryWrapper();
        queryWrapper.select("setmeal_id, count(id) as value");
        queryWrapper.groupBy("setmeal_id");

        List<Map<String, Object>> hotSetmeal = new ArrayList<>();

        List<Map<String, Object>> setmeals = orderDaoMP.selectMaps(queryWrapper);

        for (Map<String, Object> setmealMp : setmeals) {
            Integer setmeal_id = (Integer) setmealMp.get("setmeal_id");
            SetmealMP s = setmealDaoMP.selectOne(new LambdaQueryWrapper<SetmealMP>().eq(SetmealMP::getId, setmeal_id));
            Map<String, Object> tem = new HashMap<>();
            tem.put("name", s.getName());
            Integer value = Integer.valueOf(setmealMp.get("value").toString());
            tem.put("setmeal_count", value);
            tem.put("proportion", new DecimalFormat("######0.00").format(value / hotSetmeals));
            hotSetmeal.add(tem);
        }
        stringObjectHashMap.put("reportDate", day);
        stringObjectHashMap.put("todayNewMember", todayNewMember);
        stringObjectHashMap.put("totalMember", totalMember);
        stringObjectHashMap.put("thisWeekNewMember", thisWeekNewMember);
        stringObjectHashMap.put("thisMonthNewMember", thisMonthNewMember);
        stringObjectHashMap.put("todayOrderNumber", todayOrderNumber);
        stringObjectHashMap.put("thisWeekOrderNumber", thisWeekOrderNumber);
        stringObjectHashMap.put("thisMonthOrderNumber", thisMonthOrderNumber);
        stringObjectHashMap.put("todayVisitsNumber", todayVisitsNumber);
        stringObjectHashMap.put("thisWeekVisitsNumber", thisWeekVisitsNumber);
        stringObjectHashMap.put("thisMonthVisitsNumber", thisMonthVisitsNumber);
        stringObjectHashMap.put("hotSetmeal", hotSetmeal);
        return stringObjectHashMap;
    }
    }
